package com.sgb.kjwl.view.ui.activity.manage

import android.content.Context
import android.content.Intent
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppActivity
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.ActivityAgentMonitoringReportBinding
import com.sgb.kjwl.view.ui.fragment.manage.OtherInquiryFragment

/**
 * 举报监督(企业/团队)
 */
class AgentMonitoringReportActivity : AppActivity() {

    private lateinit var mBinding : ActivityAgentMonitoringReportBinding
    private lateinit var mAdapter : MonitoringReportPageAdapter

    companion object{
        private  val mTitles = arrayOf("收到的举报", "发起的举报")
        fun launch(context: Context){
            val intent = Intent(context, AgentMonitoringReportActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun initView() {
        super.initView()
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_agent_monitoring_report)
        mBinding.include.titleView.text = "举报监督"
        mBinding.include.ivBack.setOnClickListener { finish() }
        renderTab()
     }

    private fun renderTab() {
        for (item in mTitles) {
            val tab: TabLayout.Tab = mBinding.tablayout.newTab()
            tab.text = item
            mBinding.tablayout.addTab(tab)
        }
        mBinding.tablayout.getTabAt(0)!!.select()
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.tablayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {

            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })
        mAdapter = MonitoringReportPageAdapter(supportFragmentManager)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = mTitles.size
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        mBinding.viewPage.currentItem = 0
    }


    class MonitoringReportPageAdapter(fm : FragmentManager) : FragmentPagerAdapter(fm) {
        override fun getCount(): Int {
            return mTitles.size
        }

        override fun getItem(position: Int): Fragment {
            if (position == 0){
                return OtherInquiryFragment.newInstance()
            }
            return OtherInquiryFragment.newInstance()
        }

        override fun getPageTitle(position: Int): CharSequence {
            return mTitles[position]
        }
    }

}